package com.jsgt.greekfire.common.util

import com.fhx.core.utils.SharePreferencesUtil
import com.jsgt.greekfire.*
import com.jsgt.greekfire.bean.AssetsBean
import com.jsgt.greekfire.bean.WalletInfoBean
import com.jsgt.greekfire.event.AssetsEvent
import com.jsgt.greekfire.event.USDTPriceEvent
import com.jsgt.greekfire.event.WalletInfoEvent
import com.jsgt.greekfire.common.http.RetrofitManager
import com.jsgt.greekfire.repository.CoinRepository
import com.jsgt.greekfire.repository.UserRepository
import com.jsgt.greekfire.repository.WalletRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus

object NetDataManager {
    private val userRepository = UserRepository.instance
    private val coinRepository = CoinRepository.instance
    private val walletRepository = WalletRepository.instance
    var assetsList: List<AssetsBean> = ArrayList()
    var walletInfo: WalletInfoBean? = null

    //把之前数据清空
    fun logout() {
        assetsList = ArrayList()
        walletInfo = null
    }

    var reqAssets = false//正在请求资产
    var getPrice = false//正在请求资产
    val result: String? = null

    @Synchronized
    fun getUserAssets() {
        if (reqAssets) {
            return
        }
        reqAssets = true
        GlobalScope.launch(Dispatchers.IO) {
            try {
                val data = userRepository.userAssetList()
                if (data.code == 0 && data.list != null) {
                    assetsList = data.list
                    for (item in assetsList) {
//                        when (item.assetGuid){
//                            "1" -> {
//                                item.assetName = "GLB"
//                            }
//                            "2" -> {
//                                item.assetName = "USDT"
//                            }
//                            "3" -> {
//                                item.assetName = "UTIT"
//                            }
//                            "4" -> {
//                                item.assetName = "UT"
//                            }
//                            else->{
//                                item.assetName = ""
//                            }
//                        }
                    }
                    EventBus.getDefault().post(AssetsEvent(0))
                } else {
                    EventBus.getDefault().post(AssetsEvent(1))
                }
            } catch (e: Exception) {
                EventBus.getDefault().post(AssetsEvent(2))
                RetrofitManager.processNetException(e, null)
                if (BuildConfig.DEBUG) {
                    e.printStackTrace()
                }
            }
            reqAssets = false
        }
    }

    var reqUSTDPrice = false
    fun getUSTDPrice() {
        if (reqUSTDPrice) {
            return
        }
        reqUSTDPrice = true
        GlobalScope.launch(Dispatchers.IO) {
            try {
                val data = coinRepository.getUSTDPrice()
                if (data.code == 0) {
                    MONEY_SCALE = data.result!!
                    MONEY_UTIT_SCALE = data.current?.conDecimal!!
                    SharePreferencesUtil.saveFloat(
                        App.context,
                        MONEY_UTIT_SCALE_KEY,
                        MONEY_UTIT_SCALE
                    )
                    SharePreferencesUtil.saveFloat(
                        App.context,
                        MONEY_SCALE_KEY,
                        MONEY_SCALE.toFloat()
                    )
                    EventBus.getDefault().post(USDTPriceEvent())
                }
            } catch (e: Exception) {
                if (BuildConfig.DEBUG) {
                    e.printStackTrace()
                }
                //有些异常例如 failed to connect to apiv3.utios.me这种，在拦截器里好像处理不到，就在这里捕获
                RetrofitManager.processNetException(e, null)
            }
            reqUSTDPrice = false
        }
    }

    var reqWalletInfo = false
    fun getWalletInfo() {
        if (reqWalletInfo) {
            return
        }
        reqWalletInfo = true
        GlobalScope.launch(Dispatchers.IO) {
            try {
                val data = walletRepository.getWalletInfo()
                if (data.code == 0 && data.result != null) {
                    walletInfo = data.result
                    IS_ACTIVATION = walletInfo?.status!!
                    WALLET_ADDRESS = walletInfo?.utiAddress!!
                    SharePreferencesUtil.saveInteger(
                        App.context!!,
                        ACTIVATION_STATUS,
                        IS_ACTIVATION
                    )
                    EventBus.getDefault().post(WalletInfoEvent())
                }
            } catch (e: Exception) {
                if (BuildConfig.DEBUG) {
                    e.printStackTrace()
                }
            }
            reqWalletInfo = false
        }
    }

    fun getCountAsset(): Double {
        var totalBalance = 0.0
        for (i in assetsList) {
            var sum = 0.0
            when (i.assetGuid) {
                "1" -> {
                    sum = (i.assetBalance!! + i.assetFrozen!!) * 0.1 * MONEY_SCALE
                }
                "2" -> {
                    sum = (i.assetBalance!! + i.assetFrozen!!) * MONEY_SCALE
                }
                "3" -> {
                    sum = (i.assetBalance!! + i.assetFrozen!!) * 0.1 * MONEY_SCALE
                }
            }
            totalBalance += sum
        }
        return totalBalance
    }

    fun findAssetsBean(guid: String): AssetsBean? {
        for (item in assetsList) {
            if (item.assetGuid == guid) {
                return item
            }
        }
        return null
    }


}